﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SmallERP.Bll.Com;
using SmallERP.COM;
using SmallERP.DAL;
using SmallERP.MODEL.FenPian;
using SmallERP.MODEL.FoundationModel;
using Smallerp.Contracts;
using System.Data;
using Smallerp.Contracts.DataContracts;

namespace SmallERP.Bll.FenPian
{
    public class InnerReturnBLL
    {
        IOperateDBFenPian _IOperateDB;
        public InnerReturnBLL(IOperateDBFenPian inop)
        {
            _IOperateDB = inop;
        }
        //public PhotoView GetPhotoView(int inPhotoID)
        //{
        //    PhotoView view = new PhotoView();
        //    using (smallerpDataContext db = new smallerpDataContext())
        //    {
        //        photoInfo photo = db.photoInfo.FirstOrDefault(p => p.PhotoId == inPhotoID);
        //        if (photo == null)
        //        {
        //            return null;
        //        }

        //        photo.orderInfoReference.Load();
        //        orderInfo orderInfo = photo.orderInfo;
        //        view.OrderProductId = orderInfo.OrderProductId;
        //        view.PhotoId = photo.PhotoId;
        //        view.Width = photo.Width;
        //        view.Height = photo.Height;
        //        view.PicPath = photo.PicPath;
        //        view.ProductName = orderInfo.OrderName;
        //        view.PhotoName = photo.PhotoName;
        //    }
        //    return view;
        //}
        public InnerReturn_InfoDCT GetInnerView(int inPhotoID)
        {
            InnerReturn_InfoDCT view = new InnerReturn_InfoDCT();
            string QueryInnerReturn = "select orderinfo.OrderProductId,photoid,photoinfo.width,photoinfo.height,photoinfo.picpath,orderName,photoname,photoinfo.status from orderinfo,photoinfo where  orderinfo.orderproductid=photoinfo.orderproductid and photoid=" + inPhotoID;
            DataTable table = _IOperateDB.QueryDataTable(QueryInnerReturn);
            if (table.Rows.Count > 0)
            {
                view.OrderProductId = (int)table.Rows[0]["OrderProductId"];
                view.PhotoId = (int)table.Rows[0]["PhotoId"];
                view.Width = (float)Convert.ToDouble(table.Rows[0]["width"]);
                view.Height = (float)Convert.ToDouble(table.Rows[0]["height"]);
                view.PicPath = (string)table.Rows[0]["PicPath"];
                view.OrderName = (string)table.Rows[0]["OrderName"];
                view.PhotoName = (string)table.Rows[0]["PhotoName"];
                view.PhotoStatus = (int)table.Rows[0]["Status"];
            }
            else
            {
                view = null;
            }
            //using (smallerpDataContext db = new smallerpDataContext())
            //{


            //    photoInfo photo = db.photoInfo.FirstOrDefault(p => p.PhotoId == inPhotoID);
            //    if (photo == null)
            //    {
            //        return null;
            //    }

            //    photo.orderInfoReference.Load();
            //    orderInfo order = photo.orderInfo;
            //    view.OrderProductId = order.OrderProductId;
            //    view.PhotoId = photo.PhotoId;
            //    view.Width = (float)photo.Width;
            //    view.Height = (float)photo.Height;
            //    view.PicPath = photo.PicPath;
            //    view.OrderName = order.OrderName;
            //    view.PhotoName = photo.PhotoName;
            //    view.PhotoStatus = photo.Status;
            //}
            return view;
        }
        /// <summary>
        /// 创建返工单
        /// 
        /// </summary>
        /// <param name="inView"></param>
        /// <param name="inRemark"></param>
        /// <returns></returns>
        //public int CreateInnerReturn(List<PhotoView> inView, string inrResponName, string inCreateName, string inRemark)
        //{
        //    using (smallerpDataContext db = new smallerpDataContext())
        //    {
        //        try
        //        {


        //            Users user = db.Users.FirstOrDefault(p => p.UserName == inrResponName);
        //            if (user == null)
        //            {
        //                Users useAdd = new Users();
        //                useAdd.UserName = inrResponName;
        //                db.AddToUsers(useAdd);
        //            }
        //            Users user1 = db.Users.FirstOrDefault(p => p.UserName == inCreateName);
        //            if (user1 == null)
        //            {
        //                Users useAdd = new Users();
        //                useAdd.UserName = inCreateName;
        //                db.AddToUsers(useAdd);
        //            }
        //            InnerReturnStyle style = db.InnerReturnStyle.FirstOrDefault(p => p.InnerReturnStyleName == inRemark);
        //            if (style == null)
        //            {
        //                InnerReturnStyle styleAdd = new InnerReturnStyle();
        //                styleAdd.InnerReturnStyleName = inRemark;
        //                db.AddToInnerReturnStyle(styleAdd);
        //            }
        //            InnerReturn innerReturn = new InnerReturn();
        //            foreach (PhotoView view in inView)
        //            {

        //                ;
        //                photoInfo photo = db.photoInfo.FirstOrDefault(p => p.PhotoId == view.PhotoId);
        //                photo.orderInfoReference.Load();
        //                orderInfo order = photo.orderInfo;
        //                innerReturn.PhotoId = photo.PhotoId;
        //                innerReturn.OrderproductID = view.OrderProductId;
        //                innerReturn.Remark = inRemark;
        //                innerReturn.PicPath = photo.PicPath;
        //                innerReturn.CreateMan = inCreateName;
        //                innerReturn.ResponMan = inrResponName;
        //                innerReturn.Width = photo.Width;
        //                innerReturn.Heigth = photo.Height;
        //                innerReturn.CreateTime = UnityMethod.GetSystemTime();
        //                innerReturn.PhotoAreas = photo.Width * photo.Height * 2.54 * 2.54 / 10000;
        //                //innerReturn.
        //                order.Status = (int)Status.OrderStatus.Doing;
        //                photo.Status = (int)Status.PhotoStatus.WaitPB;
        //                photo.IsReback = true;
        //                db.AddToInnerReturn(innerReturn);
        //            }
        //        }
        //        catch
        //        {
        //            return -1;
        //        }


        //        db.SaveChanges();
        //        return 0;
        //    }
        //}
        /// <summary>
        /// 取得所有人名字
        /// </summary>
        /// <returns></returns>
        public List<string> GetAllName()
        {
            List<string> listName = new List<string>();
            //using (smallerpDataContext db = new smallerpDataContext())
            //{
            //    List<Users> userList = db.Users.ToList();
            //    foreach (Users user in userList)
            //    {
            //        listName.Add(user.UserName);
            //    }
            //}
            string queryName = "select * from Users";
            DataTable usertable = _IOperateDB.QueryDataTable(queryName);
            for (int i = 0; i < usertable.Rows.Count; i++)
            {
                listName.Add(usertable.Rows[i]["UserName"].ToString());
            }
                return listName;
        }

        //public List<string> GetAllInnerStyleName()
        //{
        //    List<string> InnerReturnStyleName = new List<string>();
        //    using (smallerpDataContext db = new smallerpDataContext())
        //    {
        //        List<InnerReturnStyle> userList = db.InnerReturnStyle.ToList();
        //        foreach (InnerReturnStyle list in userList)
        //        {
        //            InnerReturnStyleName.Add(list.InnerReturnStyleName);
        //        }
        //    }
        //    return InnerReturnStyleName;
        //}
        public List<string> GetAllPaperWasteType()
        {
            List<string> PaperWasteTypeList = new List<string>();
            //using (smallerpDataContext db = new smallerpDataContext())
            //{
            //    List<PaperWasteType> userList = db.PaperWasteType.ToList();
            //    foreach (PaperWasteType list in userList)
            //    {
            //        PaperWasteTypeList.Add(list.PaperOperationTypeName);
            //    }
            //}
            string queryName = "select * from PaperWasteType";
            DataTable usertable = _IOperateDB.QueryDataTable(queryName);
            for (int i = 0; i < usertable.Rows.Count; i++)
            {
                PaperWasteTypeList.Add(usertable.Rows[i]["PaperOperationTypeName"].ToString());
            }
           
            return PaperWasteTypeList;
        }

        public int CreateInnerReturn(List<InnerReturn_InfoDCT> CurrPhotoList, out Dictionary<int, int> outPhotoDict)
        {
            return _IOperateDB.CreateInnerReturn(CurrPhotoList, out outPhotoDict);
        }

    }
}
